﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class ConfirmProductReceive : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillApplicationProductGridView();
            }
        }

        private void FillApplicationProductGridView()
        {
            ApplicationService serivce = new ApplicationService();
            long productID = Convert.ToInt64(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]);
            long formID = serivce.GetFormIDByProductID(productID);
            ApplicationForm applicationForm = serivce.GetApplicationFormDetailByID(formID);

            // bind user information
            applyCompanyNameTxtBox.Text = applicationForm.User.FullName; //ContextService.CurrentAccount.CompanyName;
            mailAddressTxtBox.Text = applicationForm.User.UserAddress; //ContextService.CurrentAccount.UserAddress;
            applierEmailTxtBox.Text = applicationForm.User.UserEmail; //ContextService.CurrentAccount.UserEmail;
            applierFaxTxtBox.Text = applicationForm.User.UserFax; //ContextService.CurrentAccount.UserFax;
            //rmaLabel.Text = applicationForm.ApplicationCode;
            runningNumberLabel.Text = applicationForm.RunningNumber;
            //receiveLabel.Text = applicationForm.ReceiveDateTime.HasValue ? applicationForm.ReceiveDateTime.Value.ToString("yyyy-MM-dd") : "0000-00-00";
            //applierMobilePhoneTxtBox.Text = ContextService.CurrentAccount.UserMobilePhone;
            applierNameTxtBox.Text = applicationForm.User.ContactPerson; //ContextService.CurrentAccount.UserName;
            applierPhoneTxtBox.Text = applicationForm.User.UserPhone; //ContextService.CurrentAccount.UserPhone;
            statusLabel.Text = applicationForm.Status;

            // bind product
            ProductGridView.DataSource = applicationForm.ApplicationProduct.Where(p => p.ApplicationProductID == productID);
            ProductGridView.DataBind();

            ApplicationProduct product = applicationForm.ApplicationProduct.Where(p => p.ApplicationProductID == productID).FirstOrDefault();
            if (!product.RequisitionFormReference.IsLoaded)
            {
                product.RequisitionFormReference.Load();
            }
            if (!product.RequisitionForm.LogisticCompanyReference.IsLoaded)
            {
                product.RequisitionForm.LogisticCompanyReference.Load();
            }
            if (product.RequisitionForm != null)
            {
                wlgsLbl.Text = product.RequisitionForm.LogisticCompany.Name;
                wlNOLbl.Text = product.RequisitionForm.DistributionFormID;
                sendTimeLbl.Text = product.SendBackTime.HasValue ? product.SendBackTime.Value.ToString("yyyy-MM-dd") : "";
                receiveTimeTextBox.Text = product.ClientReceiveDateTime.HasValue ? product.ClientReceiveDateTime.Value.ToString("yyyy-MM-dd") : DateTime.Now.ToString("yyyy-MM-dd");
                if (!product.UserReference.IsLoaded)
                {
                    product.UserReference.Load();
                }
                if (product.User != null)
                {
                    engineerLbl.Text = product.User.ContactPerson;
                }
                else
                {
                    engineerLbl.Text = "N/A";
                }
            }
        }

        protected void okLinkButton_Click(object sender, EventArgs e)
        {
            ApplicationProductService productService = new ApplicationProductService();
            long productID = Convert.ToInt64(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]);
            ApplicationProduct product = productService.GetApplicationProductByID(productID);
            product.ClientReceiveDateTime = Convert.ToDateTime(receiveTimeTextBox.Text.Trim());
            productService.UpdateProduct(product);

            if (!product.ApplicationFormReference.IsLoaded)
            {
                product.ApplicationFormReference.Load();
            }
            ApplicationService appService = new ApplicationService();
            appService.ClientReceiveAllProducts(product.ApplicationForm.ApplicationID);

            Response.Write(string.Format("<script language='JavaScript'> alert( '收获日期已成功保存！');location.href= '{0}' </script> ",
                "ListProductHaveSent.aspx"));
        }
    }
}
